home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 40 / Amiga Format CD40 (1999-05-11)(Future Publishing)(GB)(Track 1 of 3)[!][issue 1999-06].iso / -readerstuff- / iain_hamilton / risk.c < prev   
C/C++ Source or Header  |  1999-03-27  |  13KB  |  482 lines

  1. /*Risk Probability Calculator
  2.   By Iain Hamilton Nov 98
  3.   iain@slarti.demon.co.uk
  4.   http://www.slarti.demon.co.uk */
  5.  
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <math.h>
  9.  
  10. #define RANDMAX 6
  11.  
  12. void prob32(void);
  13. void prob31(void);
  14. void prob22(void);
  15. void prob21(void);
  16. void prob11(void);
  17. void clrscr();
  18.  
  19. main()
  20. {
  21.     int exitcode = 0;
  22.     char choice;
  23.  
  24. /* menu code starts here */
  25.  
  26.     while (exitcode == 0)
  27.     {
  28.         clrscr();
  29.         printf("\n\t\tProbability Calculator for Dice Rolls in Risk\n");
  30.         printf("\n\t\t\tWritten Nov98 by Iain Hamilton\n");
  31.         printf("\n\t\tPlease Choose an option from the menu..\n\n");
  32.  
  33.         printf("\t1...Begin Calculation for 3 attacking vs 2 defending\n\n");
  34.         printf("\t2...Begin Calculation for 3 attacking vs 1 defending\n\n");
  35.         printf("\t3...Begin Calculation for 2 attacking vs 2 defending\n\n");
  36.         printf("\t4...Begin Calculation for 2 attacking vs 1 defending\n\n");
  37.         printf("\t5...Begin Calculation for 1 attacking vs 1 defending\n\n");
  38.         printf("\t6...Exit the Program\n\n");
  39.  
  40.         printf("\t\tPlease make your choice with the appropriate key : ");
  41.  
  42.         /*  Read the Keypress */
  43.  
  44.         choice = getch();
  45.         clrscr();
  46.         switch (choice)
  47.         {
  48.             case '1' : prob32();
  49.                  break;
  50.             case '2' : prob31();
  51.                        break;
  52.             case '3' : prob22();
  53.                        break;
  54.             case '4' : prob21();
  55.                        break;
  56.             case '5' : prob11();
  57.                        break;
  58.             case '6' : exitcode = 1;
  59.                        break;
  60.             default  : clrscr();
  61.                        printf("\nChoice out of range, please try again...");
  62.                        break;
  63.         }
  64.         printf("\n\n\t\tPress a key to continue....");
  65.      getch();
  66.      getch();
  67.      }
  68. }
  69.  
  70. /* calculate rolls of the dice and calculate probability */
  71.  
  72. void prob32(void)
  73. {
  74.  
  75.     /* define variables */
  76.  
  77.     int attackdice1 = 0;
  78.     int attackdice2 = 0;
  79.     int attackdice3 = 0;
  80.     int defenddice1 = 0;
  81.     int defenddice2 = 0;
  82.     float defwincount = 0;
  83.     float attwincount = 0;
  84.     float drawcount = 0;
  85.     long int throwcount = 0;
  86.     int atlodice = 0;
  87.     int athidice = 0;
  88.     int dehidice = 0;
  89.     int delodice = 0;
  90.     int startval = 0;
  91.     float numthrows = 0;
  92.  
  93.     clrscr();
  94.  
  95.     /* get values from user */
  96.  
  97.     printf("\nEnter a start value for the Pseudorandomizer, between 0 and 5000 :");
  98.     scanf("%d", &startval);
  99.     srand(startval);
  100.     printf("\n\t\tHow many times shall I roll the dice :");
  101.     scanf("%ld", &throwcount);
  102.     printf("\n\t\tCalculating for ");
  103.     printf("%ld", throwcount);
  104.     printf(" Dice Rolls\n");
  105.     numthrows = throwcount;
  106.     while (throwcount > 0)
  107.     {
  108.  
  109.         /* roll the dice */
  110.  
  111.         attackdice1 = (rand()%RANDMAX)+1;
  112.         attackdice2 = (rand()%RANDMAX)+1;
  113.         attackdice3 = (rand()%RANDMAX)+1;
  114.         defenddice1 = (rand()%RANDMAX)+1;
  115.         defenddice2 = (rand()%RANDMAX)+1;
  116.  
  117.         /* sort defending dice into highest and lowest */
  118.  
  119.         if (defenddice1 > defenddice2)
  120.         {
  121.             dehidice = defenddice1;
  122.             delodice = defenddice2;
  123.         }
  124.         else
  125.         {
  126.             dehidice = defenddice2;
  127.             delodice = defenddice1;
  128.         }
  129.  
  130.  
  131.         /* Discard lowest attacking dice and then sort to highest and lowest
  132.         (streamlined and hard to follow method (But it works)) */
  133.  
  134.         if (attackdice1 > attackdice2)
  135.         {
  136.             if (attackdice2 > attackdice3)
  137.             {
  138.                 athidice = attackdice1;
  139.                 atlodice = attackdice2;
  140.             }
  141.             else
  142.             {
  143.                 if (attackdice3 > attackdice1)
  144.                 {
  145.                     athidice = attackdice3;
  146.                     atlodice = attackdice1;
  147.                 }
  148.                 else
  149.                 {
  150.                     athidice = attackdice1;
  151.                     atlodice = attackdice3;
  152.                 }
  153.             }
  154.         }
  155.         else
  156.         {
  157.             if (attackdice1 > attackdice3)
  158.             {
  159.                 athidice = attackdice2;
  160.                 atlodice = attackdice1;
  161.             }
  162.             else
  163.             {
  164.                 if (attackdice3 > attackdice2)
  165.                 {
  166.                     athidice = attackdice3;
  167.                     atlodice = attackdice2;
  168.                 }
  169.                 else
  170.                 {
  171.                     athidice = attackdice2;
  172.                     atlodice = attackdice3;
  173.                 }
  174.             }
  175.         }
  176.  
  177.         /* calculate the winning team */
  178.  
  179.         if (athidice > dehidice && atlodice > delodice)
  180.         {
  181.             attwincount += 1;
  182.         }
  183.         else
  184.         {
  185.             if (athidice <= dehidice && atlodice <= delodice)
  186.             {
  187.                 defwincount += 1;
  188.             }
  189.             else
  190.             {
  191.                 drawcount += 1;
  192.             }
  193.         }
  194.         throwcount -=1;
  195.     }
  196.  
  197.     /* output result */
  198.  
  199.     printf ("\n\n\t\t3 Attacking Dice vs 2 Defending Dice\n");
  200.     printf ("\n\n\t\tAttacking Won : %0.0f\n", attwincount);
  201.     printf ("\n\t\tDefending Won : %0.0f\n", defwincount);
  202.     printf ("\n\t\tDrawn : %0.0f\n\n", drawcount);
  203.     printf ("\n\n\t\tProbability of Attacking Team Winning is : %0.2f",(attwincount/numthrows));
  204.     printf ("\n\t\tProbability of Defending Team Winning is : %0.2f",(defwincount/numthrows));
  205.     printf ("\n\t\tProbability of a Draw taking place is : %0.2f\n",(drawcount/numthrows));
  206. }
  207.  
  208. void prob31(void)
  209. {
  210.  
  211.     /* define variables */
  212.  
  213.     int attackdice1 = 0;
  214.     int attackdice2 = 0;
  215.     int attackdice3 = 0;
  216.     float defwincount = 0;
  217.     float attwincount = 0;
  218.     long int throwcount = 0;
  219.     int dedice = 0;
  220.     int startval = 0;
  221.     float numthrows = 0;
  222.  
  223.     clrscr();
  224.  
  225.     /* get values from user */
  226.  
  227.     printf("\nEnter a start value for the Pseudorandomizer, between 0 and 5000 :");
  228.     scanf("%d", &startval);
  229.     srand(startval);
  230.     printf("\n\t\tHow many times shall I roll the dice :");
  231.     scanf("%ld", &throwcount);
  232.     printf("\n\t\tCalculating for ");
  233.     printf("%ld", throwcount);
  234.     printf(" Dice Rolls\n");
  235.     numthrows = throwcount;
  236.     while (throwcount > 0)
  237.     {
  238.  
  239.         /* roll the dice */
  240.  
  241.         attackdice1 = (rand()%RANDMAX)+1;
  242.         attackdice2 = (rand()%RANDMAX)+1;
  243.         attackdice3 = (rand()%RANDMAX)+1;
  244.         dedice = (rand()%RANDMAX)+1;
  245.  
  246.         if (dedice >= attackdice1 && dedice >= attackdice2 && dedice >= attackdice3)
  247.         {
  248.            defwincount += 1;
  249.         }
  250.         else
  251.         {
  252.            attwincount += 1;
  253.         }
  254.         throwcount -=1;
  255.     }
  256.  
  257.     /* output result */
  258.  
  259.     printf ("\n\n\t\t3 Attacking Dice vs 1 Defending Dice\n");
  260.     printf ("\n\n\t\tAttacking Won : %0.0f\n", attwincount);
  261.     printf ("\n\t\tDefending Won : %0.0f\n", defwincount);
  262.     printf ("\n\n\t\tProbability of Attacking Team Winning is : %0.2f",(attwincount/numthrows));
  263.     printf ("\n\t\tProbability of Defending Team Winning is : %0.2f",(defwincount/numthrows));
  264.     printf ("\n\n\t\tA draw is NOT possible in this situation");
  265. }
  266.  
  267. void prob22(void)
  268. {
  269.  
  270.     /* define variables */
  271.  
  272.     int attackdice1 = 0;
  273.     int attackdice2 = 0;
  274.     int defenddice1 = 0;
  275.     int defenddice2 = 0;
  276.     float defwincount = 0;
  277.     float attwincount = 0;
  278.     float drawcount = 0;
  279.     long int throwcount = 0;
  280.     int atlodice = 0;
  281.     int athidice = 0;
  282.     int dehidice = 0;
  283.     int delodice = 0;
  284.     int startval = 0;
  285.     float numthrows = 0;
  286.  
  287.     clrscr();
  288.  
  289.     /* get values from user */
  290.  
  291.     printf("\nEnter a start value for the Pseudorandomizer, between 0 and 5000 :");
  292.     scanf("%d", &startval);
  293.     srand(startval);
  294.     printf("\n\t\tHow many times shall I roll the dice :");
  295.     scanf("%ld", &throwcount);
  296.     printf("\n\t\tCalculating for ");
  297.     printf("%ld", throwcount);
  298.     printf(" Dice Rolls\n");
  299.     numthrows = throwcount;
  300.     while (throwcount > 0)
  301.     {
  302.  
  303.         /* roll the dice */
  304.  
  305.         attackdice1 = (rand()%RANDMAX)+1;
  306.         attackdice2 = (rand()%RANDMAX)+1;
  307.         defenddice1 = (rand()%RANDMAX)+1;
  308.         defenddice2 = (rand()%RANDMAX)+1;
  309.  
  310.         /* sort defending dice into highest and lowest */
  311.  
  312.         if (defenddice1 > defenddice2)
  313.         {
  314.             dehidice = defenddice1;
  315.             delodice = defenddice2;
  316.         }
  317.         else
  318.         {
  319.             dehidice = defenddice2;
  320.             delodice = defenddice1;
  321.         }
  322.  
  323.         if (attackdice1 > attackdice2)
  324.         {
  325.             athidice = attackdice1;
  326.             atlodice = attackdice2;
  327.         }
  328.         else
  329.         {
  330.             athidice = attackdice2;
  331.             atlodice = attackdice1;
  332.         }
  333.  
  334.         /* calculate the winning team */
  335.  
  336.         if (athidice > dehidice && atlodice > delodice)
  337.         {
  338.             attwincount += 1;
  339.         }
  340.         else
  341.         {
  342.             if (athidice <= dehidice && atlodice <= delodice)
  343.             {
  344.                 defwincount += 1;
  345.             }
  346.             else
  347.             {
  348.                 drawcount += 1;
  349.             }
  350.         }
  351.         throwcount -=1;
  352.     }
  353.  
  354.     /* output result */
  355.  
  356.     printf ("\n\n\t\t2 Attacking Dice vs 2 Defending Dice\n");
  357.     printf ("\n\n\t\tAttacking Won : %0.0f\n", attwincount);
  358.     printf ("\n\t\tDefending Won : %0.0f\n", defwincount);
  359.     printf ("\n\t\tDrawn : %0.0f\n\n", drawcount);
  360.     printf ("\n\n\t\tProbability of Attacking Team Winning is : %0.2f",(attwincount/numthrows));
  361.     printf ("\n\t\tProbability of Defending Team Winning is : %0.2f",(defwincount/numthrows));
  362.     printf ("\n\t\tProbability of a Draw taking place is : %0.2f\n",(drawcount/numthrows));
  363. }
  364.  
  365.  
  366. void prob21(void)
  367. {
  368.  
  369.     /* define variables */
  370.  
  371.     int attackdice1 = 0;
  372.     int attackdice2 = 0;
  373.     float defwincount = 0;
  374.     float attwincount = 0;
  375.     long int throwcount = 0;
  376.     int dedice = 0;
  377.     int startval = 0;
  378.     float numthrows = 0;
  379.  
  380.     clrscr();
  381.  
  382.     /* get values from user */
  383.  
  384.     printf("\nEnter a start value for the Pseudorandomizer, between 0 and 5000 :");
  385.     scanf("%d", &startval);
  386.     srand(startval);
  387.     printf("\n\t\tHow many times shall I roll the dice :");
  388.     scanf("%ld", &throwcount);
  389.     printf("\n\t\tCalculating for ");
  390.     printf("%ld", throwcount);
  391.     printf(" Dice Rolls\n");
  392.     numthrows = throwcount;
  393.     while (throwcount > 0)
  394.     {
  395.  
  396.         /* roll the dice */
  397.  
  398.         attackdice1 = (rand()%RANDMAX)+1;
  399.         attackdice2 = (rand()%RANDMAX)+1;
  400.         dedice = (rand()%RANDMAX)+1;
  401.  
  402.         if (dedice >= attackdice1 && dedice >= attackdice2)
  403.         {
  404.            defwincount += 1;
  405.         }
  406.         else
  407.         {
  408.            attwincount += 1;
  409.         }
  410.         throwcount -=1;
  411.     }
  412.  
  413.     /* output result */
  414.     printf ("\n\n\t\t2 Attacking Dice vs 1 Defending Dice\n");
  415.     printf ("\n\n\t\tAttacking Won : %0.0f\n", attwincount);
  416.     printf ("\n\t\tDefending Won : %0.0f\n", defwincount);
  417.     printf ("\n\n\t\tProbability of Attacking Team Winning is : %0.2f",(attwincount/numthrows));
  418.     printf ("\n\t\tProbability of Defending Team Winning is : %0.2f",(defwincount/numthrows));
  419.     printf ("\n\n\t\tA draw is NOT possible in this situation");
  420. }
  421.  
  422. void prob11(void)
  423. {
  424.  
  425.     /* define variables */
  426.  
  427.     int attackdice1 = 0;
  428.     float defwincount = 0;
  429.     float attwincount = 0;
  430.     long int throwcount = 0;
  431.     int dedice = 0;
  432.     int startval = 0;
  433.     float numthrows = 0;
  434.  
  435.     clrscr();
  436.  
  437.     /* get values from user */
  438.  
  439.     printf("\nEnter a start value for the Pseudorandomizer, between 0 and 5000 :");
  440.     scanf("%d", &startval);
  441.     srand(startval);
  442.     printf("\n\t\tHow many times shall I roll the dice :");
  443.     scanf("%ld", &throwcount);
  444.     printf("\n\t\tCalculating for ");
  445.     printf("%ld", throwcount);
  446.     printf(" Dice Rolls\n");
  447.     numthrows = throwcount;
  448.     while (throwcount > 0)
  449.     {
  450.  
  451.         /* roll the dice */
  452.  
  453.         attackdice1 = (rand()%RANDMAX)+1;
  454.         dedice = (rand()%RANDMAX)+1;
  455.  
  456.         if (dedice >= attackdice1)
  457.         {
  458.            defwincount += 1;
  459.         }
  460.         else
  461.         {
  462.            attwincount += 1;
  463.         }
  464.         throwcount -=1;
  465.     }
  466.  
  467.     /* output result */
  468.  
  469.     printf ("\n\n\t\t1 Attacking Dice vs 1 Defending Dice\n");
  470.     printf ("\n\n\t\tAttacking Won : %0.0f\n", attwincount);
  471.     printf ("\n\t\tDefending Won : %0.0f\n", defwincount);
  472.     printf ("\n\n\t\tProbability of Attacking Team Winning is : %0.2f",(attwincount/numthrows));
  473.     printf ("\n\t\tProbability of Defending Team Winning is : %0.2f",(defwincount/numthrows));
  474.     printf ("\n\n\t\tA draw is NOT possible in this situation");
  475. }
  476.  
  477. void clrscr()
  478. {
  479.     /* this required for Compile on Amiga using VBCC, can be removed for Turbo C/++ on PC */
  480.     printf ("\f");
  481. }
  482.